//
//  ListSearch.swift
//  SampleSwiftUI
//
//  Created on 10/17/23.
//

import SwiftUI

enum FruitToken: String, Identifiable, Hashable, CaseIterable {
    case apple
    case pear
    case banana
    var id: Self { self }
}

//struct FruitToken: Identifiable, Hashable, CaseIterable {
//
//    var id = UUID()
//
//    static var allCases: [FruitToken] = [
//        FruitToken(kind: .apple),
//        FruitToken(kind: .pear),
//        FruitToken(kind: .banana),
//    ]
//
//    enum Kind {
//        case apple
//        case pear
//        case banana
//        var id: Self { self }
//    }
//
//
//    enum Hydration: String, Identifiable, Hashable, CaseIterable {
//        case hydrated
//        case dehydrated
//        var id: Self { self }
//    }
//
//
//    var kind: Kind
//    var hydration: Hydration = .hydrated
//}

enum ProductScope {
    case fruit
    case vegetable
}

private struct SearchedView: View {
    
    @State var items: [String]
    @Binding var filteredItems: [String]
    
    @Environment(\.isSearching) private var isSearching
    
    var body: some View {
        if isSearching {
            VStack {
                Text("Searching")
                    .frame(maxWidth: .infinity, alignment: .leading)
                List {
                    ForEach(filteredItems, id: \.self) { value in
                        Text(value)
                    }
                }
            }
        } else {
            List {
                ForEach(items, id: \.self) { value in
                    Text(value)
                }
            }
        }
    }
}

struct ListSearchView: View {
    let items = ["Apple", "Pear", "Banana"]
    @State var filteredItems: [String] = []
    
    @State private var searchText: String = ""
    @State private var tokens: [FruitToken] = []
//    @State private var hydrationSelection: FruitToken.Hydration = .hydrated
    @State private var scope: ProductScope = .fruit
    
    var body: some View {
        SearchedView(items: items, filteredItems: $filteredItems)
            .searchable(text: $searchText, tokens: $tokens, suggestedTokens: .constant(FruitToken.allCases)) { token in
                switch token {
                case .apple: Text("Apple")
                case .pear: Text("Pear")
                case .banana: Text("Banana")
                }
            }
//            .searchable(text: $searchText, tokens: $tokens, suggestedTokens: .constant(FruitToken.allCases)) { token in
//                Picker(selection: $hydrationSelection) {
//                    ForEach(FruitToken.Hydration.allCases) { hydration in
//                        switch hydration {
//                        case .hydrated: Text("Hydrated")
//                        case .dehydrated: Text("Dehydrated")
//                        }
//                    }
//                } label: {
//                    switch token.kind {
//                    case .apple: Text("Apple")
//                    case .pear: Text("Pear")
//                    case .banana: Text("Banana")
//                    }
//                }
//            }
            .searchScopes($scope) {
                Text("Fruit").tag(ProductScope.fruit)
                Text("Vegetable").tag(ProductScope.vegetable)
            }
            .onSubmit(of: .search) {
                print("Search text: \(searchText)")
                let matchedItems = items.filter { item in
                    item.lowercased().contains(searchText.lowercased())
                }
                filteredItems = matchedItems
            }
    }
}

struct ListSearch_Previews: PreviewProvider {
    static var previews: some View {
        ListSearchView()
    }
}
